package qsc.datastructure.tree;

/**
 * @auther QiuShangcheng
 * @create 2021/4/14
 */
public class Node {
    private Integer value;
    private String name;
    private Node left;
    private Node right;

    @Override
    public String toString() {
        return "Node{" +
                "no=" + value +
                ", name='" + name + '\'' +
                '}';
    }

    /**
     * 前序遍历
     */
    public void preOrder() {
        System.out.println(this);
        if (this.left != null) {
            this.left.preOrder();
        }
        if (this.right != null) {
            this.right.preOrder();
            ;
        }
    }

    /**
     * 中序遍历
     */
    public void infixOrder() {
        if (this.left != null) {
            this.left.infixOrder();
        }
        if (this != null) {
            System.out.println(this);
        }
        if (this.right != null) {
            this.right.infixOrder();
        }
    }

    /**
     * 后序遍历
     */
    public void postOrder() {
        if (this.left != null) {
            this.left.postOrder();
        }
        if (this.right != null) {
            this.right.postOrder();
        }
        if (this != null) {
            System.out.println(this);
        }
    }

    /**
     * 前序查找
     *
     * @param no
     * @return
     */
    public Node preSelect(int no) {
        if (this.value == no) {
            return this;
        }
        Node node = null;
        if (this.left != null) {
            node = this.left.preSelect(no);
        }
        if (node != null) {
            return node;
        }
        if (this.right != null) {
            node = this.right.preSelect(no);
        }
        return node;
    }

    /*中序查找*/
    public Node infixSelect(int no) {
        if (this.left != null) {
            return this.left.infixSelect(no);
        }
        if (this.value == no) {
            return this;
        }
        if (this.right != null) {
            return this.right.infixSelect(no);
        }
        return null;
    }

    /*后序查找*/
    public Node endSelect(int no) {
        if (this.left != null) {
            return this.left.endSelect(no);
        }
        if (this.right != null) {
            return this.right.endSelect(no);
        }
        if (this != null) {
            return this;
        }
        return null;
    }

    public int height() {
        return Math.max(left == null ? 0 : left.height(), right == null ? 0 : right.height()) + 1;
    }

    public int leftHeight() {
        if (left == null) {
            return 0;
        }
        return this.left.height();
    }

    public int rightHeight() {
        if (right == null) {
            return 0;
        }
        return this.right.height();
    }

    public void leftRotate() {
        Node newNode = new Node(value);
        newNode.left = left;
        newNode.right = this.right.left;
        value = right.getValue();
        right = right.right;
        left = newNode;
    }

    public void rightRotate() {
        Node newNode = new Node(value);
        newNode.right = right;
        newNode.left = left.right;
        value = this.left.getValue();
        this.left = left.left;
        this.right = newNode;
    }

    public void addToAVLTree(Node node) {
        if (this.value == null) {
            this.value = node.getValue();
            this.left = node.left;
            this.right = node.right;
        } else if (node.getValue() < this.value) {
            if (this.left == null) {
                this.left = node;
            } else {
                this.left.addToAVLTree(node);
            }
        } else {
            if (this.right == null) {
                this.right = node;
            } else {
                this.right.addToAVLTree(node);
            }
        }
        //实现树的平衡
        //左平衡
        if (rightHeight() - leftHeight() > 1) {
            if (right.leftHeight() > right.rightHeight()) {
                right.rightRotate();
                leftRotate();
            } else {
                leftRotate();
            }
            return;//防止左平衡以后又右平衡
        }
        //右平衡
        if (leftHeight() - rightHeight() > 1) {
            if (left.rightHeight() > left.leftHeight()) {
                left.leftRotate();
                rightRotate();
            } else {
                rightRotate();
            }
        }
    }

    public Node(Integer no, String name) {
        this.value = no;
        this.name = name;
    }

    public Node(Integer no) {
        this.value = no;
    }

    public Node getLeft() {
        return left;
    }

    public Node setLeft(Node left) {
        this.left = left;
        return this;
    }

    public Node getRight() {
        return right;
    }

    public Node setRight(Node right) {
        this.right = right;
        return this;
    }

    public Node() {
    }

    public Integer getValue() {
        return value;
    }

    public Node setValue(Integer no) {
        this.value = no;
        return this;
    }

    public String getName() {
        return name;
    }

    public Node setName(String name) {
        this.name = name;
        return this;
    }


}
